﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnWeb.DAO;
using DoAnWeb.DTO;
using DoAnWeb.BUS;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DoAnWeb
{
    public partial class PostProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TaiKhoanBUS BUS = new TaiKhoanBUS();
            if (!IsPostBack)
            {
                if (CurrentContext.IsLogged)
                {
                    if (BUS.GetKind(CurrentContext.UserName) == "LTK3")
                    {
                        LoadLoaiSanPham();
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

        public void LoadLoaiSanPham()
        {
            LoaiSanPhamDTO DTO = new LoaiSanPhamDTO();
            LoaiSanPhamBUS BUS = new LoaiSanPhamBUS();
            dlLoaiSanPham.DataSource = BUS.GetList();
            dlLoaiSanPham.DataTextField = "TenLoaiSanPham";
            dlLoaiSanPham.DataValueField = "MaLoaiSanPham";
            dlLoaiSanPham.DataBind();
        }

        
        private System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);
            g.InterpolationMode =InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (System.Drawing.Image)b;
        }

        protected void btnDangSanPham_Click(object sender, EventArgs e)
        {
            TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
            TinKhuyenMaiBUS BUS = new TinKhuyenMaiBUS();
            DTO.TieuDe = txtTieuDe.Text;
            DTO.MoTa = ckNoiDung.Text;
            DTO.DiaChi = txtDiaChi.Text;
            DTO.DienThoai = txtDienThoai.Text;
            DTO.GiaGoc = int.Parse (txtGiaGoc.Text);
            DTO.GiaBan = int.Parse(txtGiaGoc.Text)  - (int.Parse(txtGiaGoc.Text) * int.Parse(txtPhanTram.Text) / 100);
            DTO.PhanTramKhuyenMai = int.Parse(txtPhanTram.Text);
            DTO.MaLoaiSanPham = int.Parse(dlLoaiSanPham.SelectedValue.ToString());
            string uploadFolder = Server.MapPath("~/images/Cat/");
            string uploadFolderThumb = Server.MapPath("~/images/Cat/Thumb/");
            if (!Directory.Exists(uploadFolderThumb))  // if it doesn't exist, create
                Directory.CreateDirectory(uploadFolderThumb);
            if (!Directory.Exists(uploadFolder))  // if it doesn't exist, create
                Directory.CreateDirectory(uploadFolder);
            //DTO.HinhAnh = fuUploadHinhAnh.FileName;
            HttpFileCollection fc = Request.Files;
            for (int i = 0; i < fc.Count; i++)
            {
                HttpPostedFile pf = fc[i];
                if (pf.ContentLength > 0)
                {
                    string filename = Path.Combine(uploadFolder, pf.FileName);
                    DTO.HinhAnh = pf.FileName;
                    string filename1 = Path.Combine(uploadFolderThumb, pf.FileName);
                    Size size = new Size(300,200);
                    System.Drawing.Image Img = new Bitmap(fuUploadHinhAnh.PostedFile.InputStream);
                    Img =resizeImage(Img, size);
                    pf.SaveAs(filename1);
                    pf.SaveAs(filename);
                    
                    //Label1.Text += "<br> Uploadsuccessful " + pf.FileName + " Size :" + ((float)pf.ContentLength / 1000).ToString() + "Kb";
                }
            }
            DTO.NgayDang = DateTime.Parse( DateTime.Now.ToShortDateString());
            DTO.NgayHetHan = DateTime.Parse(DateTime.Now.AddDays(10).ToShortDateString());
            DTO.TenTaiKhoanDang = CurrentContext.UserName;
            bool KiemTra = BUS.Add(DTO);
            if (KiemTra)
            {
                lbThongBao.Text = "Bạn đã đăng sản phẩm thành công";
            }
            else
            {
                lbThongBao.Text = "<font color='Red'>Bạn đã đăng sản phẩm thất bại</font>";
                return;
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (fuUploadHinhAnh.FileName == "")
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }
    }
}